﻿Imports System.Configuration


Namespace Configuration

    ''' <summary>
    ''' 配置管理器
    ''' </summary>
    ''' <remarks></remarks>
    Public NotInheritable Class ConfigManager

        '配置节 名字
        Private Const SettingsName As String = "ComicService"

        Private Shared defaultInstance As ComicServiceSection


        Private Sub New()
        End Sub

        ''' <summary>
        ''' 是否存在 配置信息
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Shared ReadOnly Property HaveSetting() As Boolean
            Get
                Return SectionManager.HaveSection(SettingsName)
            End Get
        End Property

        ''' <summary>
        ''' 获取设置
        ''' </summary>
        ''' <returns>如果不存在，则返回空</returns>
        ''' <remarks></remarks>
        Public Shared Function GetSettings() As ComicServiceSection
            If defaultInstance Is Nothing Then
                defaultInstance = If(HaveSetting, SectionManager.GetSection(Of ComicServiceSection)(SettingsName), New ComicServiceSection)
            End If

            Return defaultInstance
        End Function

        ''' <summary>
        ''' 保存设置
        ''' </summary>
        ''' <param name="settings">设置参数</param>
        ''' <remarks></remarks>
        Public Shared Sub SaveSettings(ByVal settings As ComicServiceSection)
            SectionManager.SaveSection(Of ComicServiceSection)(SettingsName, settings)
        End Sub

        Public Shared Sub ResetSettings()
            Dim settings As New ComicServiceSection

            With settings
                .AuthorEmail = New StringValueConfigElement("AuthorEmail")
                .AuthorName = New StringValueConfigElement("AuthorName")
                .Copyright = New StringValueConfigElement("Copyright")
                .DefaultProvider = New StringValueConfigElement("FileProvider")
                .Providers = New ProviderSettingsCollection()
            End With


            SectionManager.SaveSection(Of ComicServiceSection)(SettingsName, settings)
        End Sub


        Public Shared Sub Refresh()
            defaultInstance = If(HaveSetting, SectionManager.GetSection(Of ComicServiceSection)(SettingsName), New ComicServiceSection)

        End Sub

    End Class



End Namespace

